var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
function initDate(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
	var data = {
			cmd:"getDataRole",
			model:"license",
			userKey:userKey,
			data:{
				rolecode:userRoles
			},
			remark:"",
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		var typeOpt = "<option value='' >学段</option>";
		var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
		var classOpt = "<option value='' classes=''>班级</option>";
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var data = res.data;
			for(var i = 0; i < data.length; i++){
				var name = "";
				if(data[i].type == "0"){
					name = "幼儿园";
				}else if(data[i].type == "1"){
					name = "小学";
				}else if(data[i].type == "2"){
					name = "中学";
				}else if(data[i].type == "3"){
					name = "高中";
				}
				typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
			}
			$("#student-type-aindex",parent.parent.document).html(typeOpt);
			$("#student-grade-aindex",parent.parent.document).html(gradeOpt);
			$("#student-class-aindex",parent.parent.document).html(classOpt);
			
			$("#student-type-aindex",parent.parent.document).unbind("change");
			$("#student-type-aindex",parent.parent.document).change(function(){
				var gradeData = null;
				var type = $("#student-type-aindex option:selected",parent.parent.document).val();
				var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
				if(type != ""){
					for(var i = 0; i < data.length; i++){
						if(data[i].type == type){
							gradeData = data[i].data;
						}
					}
					for(var i = 0; i < gradeData.length; i++){
						gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
					}
				}
				$("#student-grade-aindex",parent.parent.document).html(gradeOpt);
				
				var classOpt = "<option value='' classes=''>班级</option>";
				$("#student-class-aindex",parent.parent.document).html(classOpt);
				getTeachers();
			});
			
			$("#student-grade-aindex",parent.parent.document).unbind("change");
			$("#student-grade-aindex",parent.parent.document).change(function(){
				var classes = $("#student-grade-aindex option:selected",parent.parent.document).attr("classes").split(",");
				var classcodes = $("#student-grade-aindex option:selected",parent.parent.document).attr("classcodes").split(",");
				var classOpt = "";
				for(var j = 0; j < classes.length; j++){
					if(classcodes[j] == "" && classes[j] == ""){
						classOpt += "<option value='' classes=''>班级</option>";
					}else{
						classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
					}
				}
				$("#student-class-aindex",parent.parent.document).html(classOpt);
				getTeachers();
			});
			
			$("#student-class-aindex",parent.parent.document).unbind("change");
			$("#student-class-aindex",parent.parent.document).change(function(){
				getTeachers();
			});
		}else{
			
		}
		getTeachers();
	},null,null);
}

function getTeachers(){
	var userKey = $("#user-name",parent.parent.document).attr("key");	
	var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
	var classcode = $("#student-class-aindex option:selected",parent.parent.document).val();
	var type = $("#student-type-aindex option:selected",parent.parent.document).val();
	var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
	var islock = "1";
	var data = {
			cmd:"getTeacherByClasscode",
			model:"teacher",
			userKey:userKey,
			data:{
				rolecode:userRoles,
				classcode:classcode,
				del:islock,
				schoolcode:schoolcode,
				type:type
			},
			remark:"",
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		var content = "";
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var list = res.data;
			if(list.length > 0){
				for(var i = 0; i < list.length; i ++){
					content += "<option value='"+ list[i].name +"' teachercode='" + list[i].teachercode +"' title='" + list[i].name + "' cellphone='" + list[i].cellphone + "'>" + list[i].name + "</option>";
				}
			}else{
				content += "<option style='text-align:center;' disabled='disabled'>未查询到教师！</option>";
				$("#selectR",parent.parent.document).empty();
			}
			$("#selectL",parent.parent.document).empty();
			$("#selectL",parent.parent.document).append(content);
			selSelectedData();
		}else{
			var content = "<option style='text-align:center;' disabled='disabled'>未查询到教师！</option>";
			$("#selectL",parent.parent.document).empty();
			$("#selectL",parent.parent.document).append(content);
			$("#selectR",parent.parent.document).empty();
		}
		
	});
	
}
function selectLR(){
	var leftSel = $("#selectL",parent.parent.document);
	var rightSel = $("#selectR",parent.parent.document);
	$("#toright",parent.parent.document).bind("click",function(){		
		leftSel.find("option:selected",parent.parent.document).each(function(){
			$(this).remove().appendTo(rightSel);
		});
	});
	$("#toleft",parent.parent.document).bind("click",function(){		
		rightSel.find("option:selected",parent.parent.document).each(function(){
			$(this).remove().appendTo(leftSel);
		});
	});
	leftSel.dblclick(function(){
		$(this).find("option:selected",parent.parent.document).each(function(){
			$(this).remove().appendTo(rightSel);
		});
	});
	rightSel.dblclick(function(){
		$(this).find("option:selected",parent.parent.document).each(function(){
			$(this).remove().appendTo(leftSel);
		});
	});
};
function selSelectedData(){
	var teachercodes = $("#anotices-teachers").attr("teachercodes").split(",");
	var leftSel = $("#selectL option",parent.parent.document);
	var rightSel = $("#selectR",parent.parent.document);
	rightSel.empty();
	if(teachercodes.length > 0){
		for(var i = 0; i < teachercodes.length; i ++){
			leftSel.each(function(){
				if($(this).attr("teachercode") == teachercodes[i]){
					$(this).remove().appendTo(rightSel);
				}
			});
		}
	}
	rightSel.find("option").attr("selected",true);
}
